package cn.tedu.egnotice1.controller;

import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeAddQuery;
import cn.tedu.egnotice1.pojo.entity.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 添加资讯
     * @param noticeAddParam
     * @return
     */
    @PostMapping("add")
    public String add(NoticeAddParam noticeAddParam) {
        System.out.println("noticeAddParam = " + noticeAddParam);
        Notice  notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);
        return "添加资讯成功";
    }

    /**
     * 查询资讯
     */
    @GetMapping("list")
    public List<NoticeListVO> list(NoticeAddQuery noticeAddQuery) {
            System.out.println("noticeAddQuery = " + noticeAddQuery);
        List<NoticeListVO> noticeListVO = noticeMapper.selectNoticeList(noticeAddQuery);
        return noticeListVO;
    }

    /**
     * 删除资讯
     */
    @PostMapping("del")
    public String del(Long id) {
        System.out.println("id = " + id);
        int count = noticeMapper.deleteNotice(id);
        if (count > 0) {
            return ("删除资讯成功");
        }
        return ("删除资讯失败");
    }
}
